package com.stars.easyms.logger.support.logback.joran.action;

import ch.qos.logback.core.Appender;
import ch.qos.logback.core.joran.action.Action;
import ch.qos.logback.core.joran.action.ActionConst;
import ch.qos.logback.core.joran.spi.ActionException;
import ch.qos.logback.core.joran.spi.InterpretationContext;
import ch.qos.logback.core.util.OptionHelper;
import com.stars.easyms.logger.support.EasyMsOverride;
import org.xml.sax.Attributes;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>className: EasyMsAppenderAction</p>
 * <p>description: 重写了AppenderAction类</p>
 *
 * @author guoguifang
 * @date 2019-08-20 13:30
 * @since 1.3.0
 */
public final class EasyMsAppenderAction<E> extends Action implements EasyMsOverride {

    private static final Map<String, String> CONVERT_MAP;

    static {
        Map<String, String> convertMap = new HashMap<>(8);
        convertMap.put("ch.qos.logback.core.ConsoleAppender", "com.stars.easyms.logger.support.logback.appender.EasyMsConsoleAppender");
        convertMap.put("ch.qos.logback.core.FileAppender", "com.stars.easyms.logger.support.logback.appender.EasyMsFileAppender");
        convertMap.put("ch.qos.logback.core.rolling.RollingFileAppender", "com.stars.easyms.logger.support.logback.appender.EasyMsRollingFileAppender");
        convertMap.put("ch.qos.logback.classic.sift.SiftingAppender", "com.stars.easyms.logger.support.logback.appender.EasyMsUnsynchronizedSiftingAppender");
        CONVERT_MAP = Collections.unmodifiableMap(convertMap);
    }

    private Appender<E> appender;

    private boolean inError = false;

    @Override
    @SuppressWarnings("unchecked")
    public void begin(InterpretationContext ec, String localName, Attributes attributes) throws ActionException {
        appender = null;
        inError = false;

        String className = attributes.getValue(CLASS_ATTRIBUTE);
        if (OptionHelper.isEmpty(className)) {
            addError("Missing class name for appender. Near [" + localName + "] line " + getLineNumber(ec));
            inError = true;
            return;
        }

        try {
            addInfo("About to instantiate appender of type [" + className + "]");
            if (CONVERT_MAP.keySet().contains(className)) {
                className = CONVERT_MAP.get(className);
            }
            appender = (Appender<E>) OptionHelper.instantiateByClassName(className, ch.qos.logback.core.Appender.class, context);
            appender.setContext(context);
            String appenderName = ec.subst(attributes.getValue(NAME_ATTRIBUTE));
            if (OptionHelper.isEmpty(appenderName)) {
                addWarn("No appender name given for appender of type " + className + "].");
            } else {
                appender.setName(appenderName);
                addInfo("Naming appender as [" + appenderName + "]");
            }
            HashMap<String, Appender<E>> appenderBag = (HashMap<String, Appender<E>>) ec.getObjectMap().get(ActionConst.APPENDER_BAG);
            appenderBag.put(appenderName, appender);
            ec.pushObject(appender);
        } catch (Exception oops) {
            inError = true;
            addError("Could not create an Appender of type [" + className + "].", oops);
            throw new ActionException(oops);
        }
    }

    @Override
    public void end(InterpretationContext ec, String name) {
        if (inError) {
            return;
        }

        if (appender != null) {
            appender.start();
        }

        Object o = ec.peekObject();

        if (o != appender && appender != null) {
            addWarn("The object at the of the stack is not the appender named [" + appender.getName() + "] pushed earlier.");
        } else {
            ec.popObject();
        }
    }
}